import DictDataService from "~/service/module/system/dictdata";
import { Code } from "~/utils/enums";
import { form, table, addForm } from "./_form_table";
const useQuery = () => {
    const dictDataService = new DictDataService()

    const resetDataList = () => {
        form.clear()
        getDataList()
    }

    table.pageChange = (val) => {
        table.page = val
        getDataList()
    }

    table.sizeChange = (val) => {
        table.page = 1
        table.pageSize = val
        getDataList()
    }

    const getDataList = async () => {
        table.loading = true
        const queryForm = form.getBindingValue()
        const res = await dictDataService.getList({
            pageNum: table.page,
            pageSize: table.pageSize,
            ...queryForm
        })
        if (res.code === Code.SUCCESS) {
            table.list = res.rows
            table.totalCount = res.total
        }
        table.loading = false
    }

    const getDictDataById = async (id: number) => {
        const res = await dictDataService.getDictDataById(id)
        if (res.code === Code.SUCCESS) {
            addForm.setForm(res.data)
        }
    }

    return {
        resetDataList,
        getDataList,
        getDictDataById
    }

}

export default useQuery